<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('记录人')" prop="userId">
            <UserSelect v-model="form.userId" :placeholder="$t('记录人')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('更新日期')" prop="updateDate">
            <el-date-picker
              v-model="form.updateDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              disabled
              :placeholder="$t('更新日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('主要风险点概览') }}</span>
      </div>
      <el-table
        :data="form.dealRiskBillList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column :label="$t('风险点')" show-overflow-tooltip prop="content">
          <template slot-scope="scope">
            <span>{{ $t(scope.row.riskContent) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('情况')" align="center" width="250px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('情况') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealRiskBillList.' + scope.$index + '.condition'" :rules="dealRiskBillRules.condition">
              <!--              <el-checkbox-group v-model="scope.row.condition">-->
              <!--                <el-checkbox-->
              <!--                  v-for="(dict, index) in conditionOptions"-->
              <!--                  :key="index"-->
              <!--                  :label="dict.dictValue"-->
              <!--                >-->
              <!--                  {{ $t(dict.dictLabel) }}-->
              <!--                </el-checkbox>-->
              <!--              </el-checkbox-group>-->
              <el-radio-group v-model="scope.row.condition" size="mini">
                <el-radio-button
                  v-for="dict in conditionOptions"
                  :key="dict.dictValue"
                  :label="$t(dict.dictValue)"
                >
                  {{ dict.dictLabel }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </template>
        </el-table-column>

      </el-table>
      <br>
      <FileTable
        :list.sync="form.fileList"
        data-type="deal_risk"
        has-permi="deal:dealRisk"
        :business-id="autoBusinessId($route.query.id, form)"
      />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:dealRisk" />
    </div>
  </div>
</template>

<script>
    import { saveDealRisk, getDealRisk } from '@/api/sass/hxs/dealRisk'
    import { getRiskSetByType } from '@/api/sass/hxs/riskSet'
    export default {
        data() {
            return {
              // 情况字典
              conditionOptions: [],
                // 表单参数
                form: {
                  dealRiskBillList: [],
                  fileList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  updateDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
                },
              dealRiskBillRules: {
                condition: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
              }
            }
        },
        created() {
          this.getDicts('condition').then(response => {
            this.conditionOptions = response.data
            console.log(this.conditionOptions, 'this.conditionOptions')
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealRisk(data.id).then(response => {
                      if (response.data) {
                        this.form = response.data
                      }
                      this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                  // 带出模板数据
                  getRiskSetByType(this.$route.query.dealType).then(template => {
                    console.log(template, 'templatetemplate')
                    this.form.dealRiskBillList = template.data.dealRiskBillList
                    this.loading = false
                  })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    userId: this.$store.state.user.userId,
                    updateDate: new Date(),
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        this.form.updateDate = new Date()
                        saveDealRisk(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
